//
//  PageTitleContentView.m
//  PageTitleContent
//
//  Created by 好米有易 on 2017/9/13.
//  Copyright © 2017年 好米有易. All rights reserved.
//

#import "PageTitleContentView.h"
#import "PageTitleView.h"
#import "PageContentView.h"
#import "PageContenModel.h"
@interface PageTitleContentView ()

/** 标题栏 */
@property (nonatomic, strong) PageTitleView *pageTitleView;
/** 内容栏 */
@property (nonatomic, strong) PageContentView *pageContentView;

/** 标题菜单数组*/
@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) NSArray<UIViewController *> *controllers;

@end


@implementation PageTitleContentView {
    CGFloat _contentViewWidth;
    CGFloat _contentViewHeight;
    CGFloat _pageTitleHeight;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        _contentViewWidth = self.bounds.size.width;
        _pageTitleHeight = 40;
        _contentViewHeight = self.bounds.size.height - _pageTitleHeight;
        
        [self addSubview:self.pageTitleView];
        [self addSubview:self.pageContentView];
        
    }
    
    return self;
}


#pragma mark - setter -
- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    self.pageContentView.titles = titles;
    self.pageTitleView.titles = titles;
}

- (void)setControllers:(NSArray<UIViewController *> *)controllers {
    _controllers = controllers;
    self.pageContentView.controllers = controllers;
}

- (void)setInfos:(NSArray<PageContenModel *> *)infos {
      _infos = infos;
    
    NSMutableArray *titlesArr = [NSMutableArray arrayWithCapacity:3];
    NSMutableArray *controllersArr = [NSMutableArray arrayWithCapacity:3];

    [infos enumerateObjectsUsingBlock:^(PageContenModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [titlesArr addObject:obj.title];
        
        UIViewController *vc = [NSClassFromString(obj.controllerName) new];
        
        vc.view.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 40);
        
        [controllersArr addObject:vc];
        
    }];
    self.titles = titlesArr.copy;
    self.controllers = controllersArr.copy;
    
}


#pragma mark - lazy -
- (PageTitleView *)pageTitleView {
    if (_pageTitleView == nil) {
        _pageTitleView = [[PageTitleView alloc] initWithFrame:CGRectMake(0, 0, _contentViewWidth, _pageTitleHeight)];
    }
    return _pageTitleView;
}

- (PageContentView *)pageContentView {
    if (_pageContentView == nil) {
        _pageContentView = [[PageContentView alloc] initWithFrame:CGRectMake(0, _pageTitleHeight, _contentViewWidth, _contentViewHeight)];
    }
    return _pageContentView;
}


@end
